// pages/addresses/addresses.js
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk; 
var id;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaSelectedStr: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: 'TPDBZ-TH5KD-5KH4Q-HJAV7-7TVNT-ANBCO'
    });
    this.loadAddress(options.id);
    id=options.id
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  fetchPOI: function () {
    var that = this;
    // 调用接口
    qqmapsdk.reverseGeocoder({
      poi_options: 'policy=2',
      get_poi: 1,
      success: function (res) {
        console.log(res);
        that.setData({
          areaSelectedStr: res.result.address
        });
      },
      fail: function (res) {
        //         console.log(res);
      },
      complete: function (res) {
        //         console.log(res);
      }
    });
  },
  loadAddress:function(id){
    var that=this;
    wx.request({
      url: getApp().globalData.domain + 'api/receiving_addres/detail?id=' + id,
      data: {
        // id: id,
        // openid: getApp().globalData.openid,
        // token: getApp().globalData.userInfo.token
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        console.log(res)
        that.setData({
          id: res.data.id,
          name: res.data.name,
          phone_number: res.data.phone_number,
          region: res.data.region,
          detailed_address: res.data.detailed_address,
        })
      },
      fail: function (err) { },
      complete: function () { }
    });
  },
  formSubmit: function (e) {
    console.log(id)
    console.log(e.detail.value)
    var that = this;
    var formData = e.detail.value;
    // var region = that.data.areaSelectedStr
    wx.request({
      url: getApp().globalData.domain + 'api/receiving_addres/edit?id='+id,
      data: formData,
      header: {
        'Content-Type': 'application/json'
      },
      method: "POST",
      success: function (res) {
        wx.navigateBack({
          delta: 1
        })
      }
    })
  }, 
})